<?php

include dirname(dirname(__FILE__)) . '/include/common.php';

class webServiceCreateUser extends core {
    public function process() {
        $data = array();

        $data['first_name'] = strip_tags(trim($_POST['firstName']));
        $data['last_name'] = strip_tags(trim($_POST['lastName']));
        $data['email'] = $this->_cleanEmail($_POST['userEmail']);
        $data['password'] = md5($_POST['password']);
        $data['user_role'] = $_POST['userRole'];
        $data['fiance_first_name'] = strip_tags(trim($_POST['fianceFirstName']));
        $data['fiance_last_name'] = strip_tags(trim($_POST['fianceLastName']));
        $wedding_date_string = $_POST['weddingDate'];
        $wedding_date_int = strtotime($wedding_date_string);
        $wedding_date = date('Y-m-d', $wedding_date_int);
        $data['wedding_date'] = $wedding_date;
        
        //CHeck validate
        $email = $this->db->escape($data['email']);
        $user = $this->db->get_row("select * from users where email='" . $email . "'");
        if ($user) {
            if ($user->active == 0) {
                /* If userEmail is found in the database and account hasn't been activated: => error code 2 */
                $this->_response(2);
            } else {
                /* If userEmail is already registered: => return error code 5 */
                $this->_response(5);
            }
        } else {
            /* If userEmail is NOT found in the database => return error code 4 */

            $data['login_count'] = 0;
            $data['active'] = 0;
            $data['confirmation_code'] = md5($data['password'] . $data['email']);
            $data['notification'] = 0;
            $data['token'] = uniqid('', true);
            $fields = array();
            $values = array();

            $this->_preDataForInsert($data, $fields, $values);

            $fields = implode(',', $fields);
            $values = implode(',', $values);
            $result = $this->db->query("INSERT INTO `users` ({$fields}) VALUES ({$values});");
           
            if (!$result) {
                $this->_response('Oops, something went wrong. Please try again');
            } else {
                $user_id = mysql_insert_id();
                //add event
                $query = "INSERT INTO `events` (`title`,`description`,`location`,`start_date`,`start_time`,`end_time`,`canbedeleted`,`user_id`)
                      VALUES ('Wedding','','','$wedding_date','','',0,$user_id);";
                
                $this->db->query($query);
               $this->_sendMail($email, $data['confirmation_code']);
                echo json_encode(array('results' => $user_id));
            }
        }
    }
    
    protected function _sendMail($email, $confirmation_code) {
        $confirmmation_url = $this->GetAbsoluteURLFolder() . '/confirm.php?code=' . $confirmation_code;
        $subject = "Your registration with Wedding Helper";
        $content = "Thank you for submitting your registration information to Wedding Helper! 
                     <br>Please click <a href='" . $confirmmation_url . "'>here</a> to activate your account.
                    <br>The Wedding Helper Team";
        return $this->send_mail($email, $subject, $content);
    }

}

//$_POST['firstName'] = 'Test';
//$_POST['lastName'] = 'iClick';
//$_POST['fianceFirstName'] = 'Chris';
//$_POST['fianceLastName'] = 'Hoover';
//$_POST['weddingDate'] = '05/08/2013';
//$_POST['userEmail'] = 'no-reply@iclickapps.com';
//$_POST['password'] = '123456';
//$_POST['userRole'] = 2;

$processor = new webServiceCreateUser();
$processor->process();
?>